﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2014/3/16
 * 时间: 7:58
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;

namespace Step
{
	/// <summary>
	/// Description of TwoWayQueue.
	/// </summary>
	public class TwoWayQueue
	{		
		Entry first;
		Entry one;
		Entry top;
		
		public void Push(object data) {
			top = new Entry(top, data);
			if(first == null) {
				first = one = top;
			}
		}
		
		public void Reset() {
			first = one;
		}
		
		public object Forword() {
			if(first == null) throw new InvalidOperationException();
			object result = first.data;
			//first.priv = first;
			first = first.next;
			
			return result;
		}
		
		public object Back() {
			if(first == null) throw new InvalidOperationException();
			object result = first.data;
			//first.next = first;
			first = first.priv;			
			
			return result;
		}
		
		class Entry
		{
			public Entry priv;
			public Entry next;
			public object data;
			
			public Entry(Entry top, object data) {
				this.data = data;
				
				if(top == null) {					
					top = this;
					top.next = this;
					top.priv = this;
				} else {
					top.next = this;
					this.priv = top;					
				}
			}
		}
	}
}
